package com.bcmusic.user.controller;

import com.bcmusic.user.config.TokenDecode;
import com.bcmusic.user.pojo.DistributorIdentification;
import com.bcmusic.user.pojo.EnterpriseIdentification;
import com.bcmusic.user.pojo.User;
import com.bcmusic.user.service.DistributorIdentificationService;
import com.bcmusic.user.service.UserService;
import com.github.pagehelper.PageInfo;
import com.sun.xml.internal.bind.v2.model.core.ID;
import entity.IdWorker;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/distributorIdentification")
@CrossOrigin
public class DistributorIdentificationController {

    @Autowired
    private DistributorIdentificationService distributorIdentificationService;

    @Autowired
    private UserService userService;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private TokenDecode tokenDecode;

    /***
     * DistributorIdentification分页条件搜索实现
     * @param distributorIdentification
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) DistributorIdentification distributorIdentification, @PathVariable int page, @PathVariable int size) {
        //调用DistributorIdentificationService实现分页条件查询DistributorIdentification
        PageInfo<DistributorIdentification> pageInfo = distributorIdentificationService.findPage(distributorIdentification, page, size);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * DistributorIdentification分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        //调用DistributorIdentificationService实现分页查询DistributorIdentification
        PageInfo<DistributorIdentification> pageInfo = distributorIdentificationService.findPage(page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param distributorIdentification
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<DistributorIdentification>> findList(@RequestBody(required = false) DistributorIdentification distributorIdentification) {
        //调用DistributorIdentificationService实现条件查询DistributorIdentification
        List<DistributorIdentification> list = distributorIdentificationService.findList(distributorIdentification);
        return new Result<List<DistributorIdentification>>(true, StatusCode.OK, "查询成功", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Long id) {
        //调用DistributorIdentificationService实现根据主键删除
        distributorIdentificationService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /***
     * 修改DistributorIdentification数据
     * @param distributorIdentification
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody DistributorIdentification distributorIdentification, @PathVariable Long id) {
        //设置主键值
        distributorIdentification.setIdentificationId(id);
        //调用DistributorIdentificationService实现修改DistributorIdentification
        distributorIdentificationService.update(distributorIdentification);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /***
     * 新增DistributorIdentification数据
     * @param distributorIdentification
     * @return
     */
    @PostMapping
    public Result add(@RequestBody DistributorIdentification distributorIdentification) {
        //调用DistributorIdentificationService实现添加DistributorIdentification
        distributorIdentificationService.add(distributorIdentification);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /***
     * 根据ID查询DistributorIdentification数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DistributorIdentification> findById(@PathVariable Long id) {
        //调用DistributorIdentificationService实现根据主键查询DistributorIdentification
        DistributorIdentification distributorIdentification = distributorIdentificationService.findById(id);
        return new Result<DistributorIdentification>(true, StatusCode.OK, "查询成功", distributorIdentification);
    }

    /***
     * 查询DistributorIdentification全部数据
     * @return
     */
    @GetMapping
    public Result<List<DistributorIdentification>> findAll() {
        //调用DistributorIdentificationService实现查询所有DistributorIdentification
        List<DistributorIdentification> list = distributorIdentificationService.findAll();
        return new Result<List<DistributorIdentification>>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 分发商信息认证
     * 呜呜呜呜
     */
    @Transactional
    @ApiOperation(notes = "添加人员,修改信息", value = "addPerson")
    @PostMapping("authentication")
    public Result<DistributorIdentification> authentication(@RequestBody @ApiParam(name = "distributorIdentification", value = "aaaaaa") DistributorIdentification distributorIdentification) {
        String userNum = tokenDecode.getUserInfo().get("id");
        User user = userService.findUserByNum(userNum);
        String photo = null;
        if (user.getUserIdentiId() != 0) {
            DistributorIdentification old = new DistributorIdentification();
            old.setValidStatus(-2);
            old.setIdentificationId(user.getUserIdentiId());
            old = distributorIdentificationService.updateSelective(old);
            photo = old.getProfilePhoto();
        }

        distributorIdentification.setDistributorNumber(userNum);
        distributorIdentification.setSubmitTime(new Date());
        distributorIdentification.setSubmitterNumber(userNum);
        distributorIdentification.setProfilePhoto(photo);
        distributorIdentificationService.add(distributorIdentification);

        user.setUserIdentiId(distributorIdentification.getIdentificationId());
        user.setUserType(3);
        userService.update(user);
        return new Result<DistributorIdentification>(true, StatusCode.OK, "查询成功", distributorIdentification);
    }
}
